package com.example.smartwaterapi.controller;

import com.example.smartwaterapi.common.api.RestApiResponse;
import com.example.smartwaterapi.service.WaterReminderSchedulerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 饮水提醒控制器
 */
@Slf4j
@Tag(name = "饮水提醒", description = "饮水提醒相关接口")
@RestController
@RequestMapping("/api/water-reminder")
public class WaterReminderController {

    @Autowired
    private WaterReminderSchedulerService waterReminderSchedulerService;

    @Operation(summary = "手动触发早上饮水提醒", description = "手动触发早上饮水提醒，用于测试")
    @PostMapping("/trigger/morning")
    public RestApiResponse<String> triggerMorningReminder() {
        try {
            log.info("手动触发早上饮水提醒");
            waterReminderSchedulerService.sendMorningWaterReminder();
            return RestApiResponse.ok("早上饮水提醒已触发");
        } catch (Exception e) {
            log.error("触发早上饮水提醒失败", e);
            return RestApiResponse.businessError("50001", "触发失败: " + e.getMessage());
        }
    }

    @Operation(summary = "手动触发中午饮水提醒", description = "手动触发中午饮水提醒，用于测试")
    @PostMapping("/trigger/noon")
    public RestApiResponse<String> triggerNoonReminder() {
        try {
            log.info("手动触发中午饮水提醒");
            waterReminderSchedulerService.sendNoonWaterReminder();
            return RestApiResponse.ok("中午饮水提醒已触发");
        } catch (Exception e) {
            log.error("触发中午饮水提醒失败", e);
            return RestApiResponse.businessError("50001", "触发失败: " + e.getMessage());
        }
    }

    @Operation(summary = "手动触发晚上饮水提醒", description = "手动触发晚上饮水提醒，用于测试")
    @PostMapping("/trigger/evening")
    public RestApiResponse<String> triggerEveningReminder() {
        try {
            log.info("手动触发晚上饮水提醒");
            waterReminderSchedulerService.sendEveningWaterReminder();
            return RestApiResponse.ok("晚上饮水提醒已触发");
        } catch (Exception e) {
            log.error("触发晚上饮水提醒失败", e);
            return RestApiResponse.businessError("50001", "触发失败: " + e.getMessage());
        }
    }

    @Operation(summary = "手动触发睡前饮水提醒", description = "手动触发睡前饮水提醒，用于测试")
    @PostMapping("/trigger/bedtime")
    public RestApiResponse<String> triggerBedtimeReminder() {
        try {
            log.info("手动触发睡前饮水提醒");
            waterReminderSchedulerService.sendBedtimeWaterReminder();
            return RestApiResponse.ok("睡前饮水提醒已触发");
        } catch (Exception e) {
            log.error("触发睡前饮水提醒失败", e);
            return RestApiResponse.businessError("50001", "触发失败: " + e.getMessage());
        }
    }

    @Operation(summary = "发送自定义饮水提醒", description = "发送自定义内容的饮水提醒")
    @PostMapping("/send/custom")
    public RestApiResponse<String> sendCustomReminder(
            @Parameter(description = "事件类型", example = "爱心提醒") @RequestParam String eventType,
            @Parameter(description = "提醒内容", example = "该喝水啦！") @RequestParam String reminderText) {
        try {
            log.info("发送自定义饮水提醒: eventType={}, reminderText={}", eventType, reminderText);
            waterReminderSchedulerService.sendCustomWaterReminder(eventType, reminderText);
            return RestApiResponse.ok("自定义饮水提醒已发送");
        } catch (Exception e) {
            log.error("发送自定义饮水提醒失败", e);
            return RestApiResponse.businessError("50001", "发送失败: " + e.getMessage());
        }
    }
}